package com.jumi.microservice.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.dto.reponse.ImportOrderReponse;
import com.jumi.microservice.entity.JmImportOrderConsignee;
import com.jumi.microservice.service.IJmImportOrderConsigneeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 海外商品实名用户表 前端控制器
 * </p>
 *
 * @author 要挑客户管理团队
 * @since 2020-10-23
 */
@RestController
@RequestMapping("/jm-import-order-consignee")
public class JmImportOrderConsigneeController  {

    @Autowired
    IJmImportOrderConsigneeService importService;

    @PostMapping("/getImportByUids")
    public List<ImportOrderReponse> getImportByUids(@RequestParam(value = "uids") List<Integer> uids) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.in("uid",uids);
        wrapper.orderByDesc("id");
        List<JmImportOrderConsignee> list = importService.list(wrapper);
        List<ImportOrderReponse> reponses=new ArrayList<>(list.size());
        for (JmImportOrderConsignee Imports : list) {
            ImportOrderReponse reponse=new ImportOrderReponse();
            BeanUtils.copyProperties(Imports,reponse);
            reponses.add(reponse);
        }
        return reponses;
    }

    @PostMapping("/addImportOrder")
    public ResponseResult addImportOrder(Integer uid, String name, String auditCode) {

        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("uid",uid);
        JmImportOrderConsignee one =  importService.getOne(wrapper);



        JmImportOrderConsignee importorder = new JmImportOrderConsignee();

        importorder.setName(name);
        importorder.setAuditCode(auditCode);
        importorder.setAddTime(LocalDateTime.now());

        if (ObjectUtil.isEmpty(one)) {
            importorder.setId(0);
        } else {
            importorder.setId(one.getId());
        }

        importorder.setUid(uid);


        return ResponseResult.success(importService.saveOrUpdate(importorder));

//        if (ObjectUtil.isNotEmpty(one)) {
//
//            UpdateWrapper<JmImportOrderConsignee> updateWrapper = new UpdateWrapper<JmImportOrderConsignee>();
//
//            updateWrapper.set("name", name);
//            updateWrapper.set("audit_code", auditCode);
//            updateWrapper.set("add_time", CheckStatus.待审核);
//
//            updateWrapper.eq("uid", uid);
//
//            return ResponseResult.success(importService.update(updateWrapper));
//
//        } else {
//
//            JmImportOrderConsignee Identity = new JmImportOrderConsignee();
//
//            Identity.setId(0);
//            Identity.setUid(uid);
//            Identity.setImage(image);
//            Identity.setRemark("");
//
//            return ResponseResult.success(importService.save(Identity));
//
//        }


    }

}
